package reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import reggie.dto.DishDto;
import reggie.entity.Dish;
import reggie.entity.DishFlavor;
import reggie.service.DishFlavorService;
import reggie.service.DishService;
import reggie.mapper.DishMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【dish(菜品管理)】的数据库操作Service实现
 * @createDate 2022-08-12 19:56:16
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
        implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品 保存对应口味
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithDishFlavor(DishDto dishDto) {
        //保存菜品到dish表
        this.save(dishDto);
        //获取菜品id
        Long dishId = dishDto.getId();


        List<DishFlavor> flavors = dishDto.getFlavors();
        //Stream流 赋值id
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //flavors.forEach(i -> i.setDishId(dishId));

        //for (DishFlavor flavor : flavors) {
        //    flavor.setDishId(dishId);
        //}
        //
        ////foreach 赋值方法
        //flavors.forEach(item -> {
        //    item.setDishId(dishId);
        //});

        //保存菜品口味数据到菜品口味表
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //拿到基本菜品
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());

        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Override
    //事务注解 保证数据的一致性
    @Transactional
    public void updateWithDishFlavor(DishDto dishDto) {
        //更新一下餐品表
        this.updateById(dishDto);
        //this.getById(dishDto);
        //清理当前口味对应的餐品数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加新的提交过来的数据数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            super.removeById(id);
        }
    }



}




